{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用基于混合高斯模型的背景提取算法，提取前景并显示(显示二值化图像，前景为白色)。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fanhui True\n"
     ]
    },
    {
     "ename": "SyntaxError",
     "evalue": "'break' outside loop (<ipython-input-1-be81971cd43e>, line 44)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-be81971cd43e>\"\u001b[1;36m, line \u001b[1;32m44\u001b[0m\n\u001b[1;33m    break\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m 'break' outside loop\n"
     ]
    }
   ],
   "source": [
    "import cv2   \n",
    "import numpy as np\n",
    "videofn=r'vtest.avi'                            #视频导入\n",
    "feature_p=dict(maxCorners=100,qualitylevel=0.3,minDistance=7,blockSize=7)             #设置角点检测参数\n",
    "#LK光流法参数\n",
    "\n",
    "lk_params = dict( winSize=(15,15),\n",
    "                  maxLevel=2,\n",
    "                  criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) \n",
    "\n",
    "cap = cv2.VideoCapture(videofn)                                                         #获取视频\n",
    "#计算第一帧特征点\n",
    "ret,prev=cap.read()                                                                     #读取第一帧，返回是否截取到的布尔值和 截取的一帧图像                                                       \n",
    "print('fanhui',ret)\n",
    "prevGray=cv2.cvtColor(prev,cv2.COLOR_BGR2GRAY)                               #转换为灰度图\n",
    "p0=cv2.goodFeaturesToTrack(prevGray,100,0.3,7)                   #得到比较好的特征点\n",
    "\n",
    "while True:\n",
    "    ret,frame=cap.read()\n",
    "    if not ret:                                                    #没读到当前帧，结束\n",
    "            break\n",
    "    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n",
    "     #计算光流\n",
    "    p1,st,err=cv2.calcOpticalFlowPyrLK(prevGray,gray,p0,None,**lk_params)\n",
    "     #选取好的跟踪点\n",
    "    goodpoints=p1[st==1]\n",
    "    goodprevpoints=p0[st==1]\n",
    "     #在结果图像中迭加画出特征点和计算出来的光流向量\n",
    "    res=frame.copy()\n",
    "    drawcolor=(0,0,255)\n",
    "    for i,(cur,prev) in enumerate(zip(goodpoints,goodprevpoints)):\n",
    "        x0,y0=cur.ravel()\n",
    "        x1,y1=prev.ravel()\n",
    "        cv2.line(res,(x0,y0),(x1,y1),drawcolor)\n",
    "        cv2.circle(res,(x0,y0),3,drawcolor)\n",
    "    ##更新上一帧\n",
    "    prevGray=gray.copy()\n",
    "    p0=goodpoints.reshape(-1,1,2)\n",
    "  \n",
    "    #显示计算结果图像\n",
    "    cv2.imshow('result',res)\n",
    "    key=cv2.waitKey(30)                                           #每一帧间隔30ms\n",
    "    if key==27:\n",
    "        break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
